package com.mxpio.erp.workshop.controller;

import com.mxpio.erp.common.jpa.model.NoticeLineKey;
import com.mxpio.erp.common.workshop.entity.OoPickingNoticeLine;
import com.mxpio.erp.common.workshop.service.OoPickingNoticeLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "OoPickingNoticeLineController", description = "委外领/退/补料通知单行接口")
@RestController
@RequestMapping("/erp/workshop/opline/")
public class OoPickingNoticeLineController {

    @Autowired
    private OoPickingNoticeLineService service;

    @GetMapping("list")
    @Operation(summary = "委外领/退/补料通知单行列表", description = "获取委外领/退/补料通知单行列表（无分页）", method = "GET")
    public Result<List<OoPickingNoticeLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OoPickingNoticeLine> ooPickingNoticeLine = service.list(OoPickingNoticeLine.class, criteria);
        return Result.OK(ooPickingNoticeLine);
    }

    @GetMapping("page")
    @Operation(summary = "委外领/退/补料通知单行列表", description = "获取委外领/退/补料通知单行列表（分页）", method = "GET")
    public Result<Page<OoPickingNoticeLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OoPickingNoticeLine> ooPickingNoticeLines = service.listPage(OoPickingNoticeLine.class, page, criteria);
        return Result.OK(ooPickingNoticeLines);
    }
    
    @GetMapping("list/{noticeNo}")
    @Operation(summary = "根据noticeNo获取委外领/退/补料通知单行", description = "根据noticeNo获取委外领/退/补料通知单行", method = "GET")
    public Result<List<OoPickingNoticeLine>> getById(@Parameter(description="业务单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, noticeNo);
        List<OoPickingNoticeLine> ooPickingNoticeLines = service.list(OoPickingNoticeLine.class, c);
        return Result.OK(ooPickingNoticeLines);
    }

    @GetMapping("list/{noticeNo}/{lineNo}")
    @Operation(summary = "根据ID获取委外领/退/补料通知单行", description = "根据ID获取委外领/退/补料通知单行", method = "GET")
    public Result<OoPickingNoticeLine> getById(@Parameter(description="业务单号") @PathVariable(name = "noticeNo", required = true) String noticeNo,@Parameter(description="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
    	NoticeLineKey lineKey = new NoticeLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setNoticeNo(noticeNo);
        OoPickingNoticeLine ooPickingNoticeLine = service.getById(OoPickingNoticeLine.class, lineKey);
        return Result.OK(ooPickingNoticeLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增委外领/退/补料通知单行", description = "新增委外领/退/补料通知单行", method = "POST")
    public Result<OoPickingNoticeLine> add(@RequestBody OoPickingNoticeLine ooPickingNoticeLine) {
        service.save(ooPickingNoticeLine);
        return Result.OK(ooPickingNoticeLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑委外领/退/补料通知单行", description = "编辑委外领/退/补料通知单行", method = "PUT")
    public Result<OoPickingNoticeLine> edit(@RequestBody OoPickingNoticeLine ooPickingNoticeLine) {
        service.update(ooPickingNoticeLine);
        return Result.OK(ooPickingNoticeLine);
    }

    @DeleteMapping("remove/{ids}")	
    @Operation(summary = "根据id删除委外领/退/补料通知单行", description = "删除委外领/退/补料通知单行,多个id用英文逗号分隔,noticeNo和lineNo间使用英文冒号分隔,noticeNo在前,lineNo在后", method = "DELETE")
    public Result<OoPickingNoticeLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            NoticeLineKey lineKey = new NoticeLineKey();
            lineKey.setNoticeNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            service.remove(OoPickingNoticeLine.class, lineKey);
        }
        return Result.OK();
    }
}
